package vworld.callback;

import generated.callback.ClientCallbackPOA;
import gui.action.PrivateChatAction;
import gui.editor.PrivateChatEditor;
import gui.editor.RoomGlobalChatEditor;
import gui.editor.RoomGlobalChatEditorInput;
import gui.entity.RoomEntity;
import gui.perspective.WorldsPerspective;
import gui.view.ConnectedUsersView;
import gui.view.GlobalChatView;
import gui.view.RoomView;
import gui.view.WorldsView;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.omg.CORBA.ORB;

import vworld.Client;

public class ClientCallbackServant extends ClientCallbackPOA {
	private ORB orb;
	private String avatarName;
	private String roomName;
	private String role;
	private String userLogin;

	/**********************
	 * Attributes accessors
	 **********************/

	public void setORB(ORB pOrb) {
		orb = pOrb;
	}

	public String avatarName() {
		return avatarName;
	}

	public void avatarName(String newAvatarName) {
		avatarName = newAvatarName;
	}

	public String roomName() {
		return roomName;
	}

	public void roomName(String newRoomName) {
		roomName = newRoomName;
	}

	public String userRole() {
		return role;
	}

	public void userRole(String newRole) {
		role = newRole;
	}

	public String userLogin() {
		return userLogin;
	}

	public void userLogin(String newUserLogin) {
		userLogin = newUserLogin;
	}

	/************************
	 * Simple users callbacks
	 ************************/

	public void doNotifyOnJoinOrExit(final String avatarName,
			final String whatHappened) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();
				IViewPart roomView = page.findViewReference(RoomView.ID)
						.getView(false);

				if (roomView != null) {
					((RoomView) roomView).getRoomViewer().setInput(
							((RoomView) roomView).createModel());
				}

				IViewPart globalChatView = page.findViewReference(
						GlobalChatView.ID).getView(false);

				if (globalChatView != null) {
					((GlobalChatView) globalChatView).receiveSpecialMessage(
							whatHappened, SWT.COLOR_GRAY);
				}
			}
		});
	}

	public void doNotifyOnMoodChange(String incomingAvatarName,
			final String whatHappened) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();

				IViewPart roomView = page.findViewReference(RoomView.ID)
						.getView(false);

				if (roomView != null) {
					((RoomView) roomView).getRoomViewer().refresh();
				}

				IViewPart globalChatView = page.findViewReference(
						GlobalChatView.ID).getView(false);

				if (globalChatView != null) {
					((GlobalChatView) globalChatView).receiveSpecialMessage(
							whatHappened, SWT.COLOR_GREEN);
				}
			}
		});
	}

	public void doNotifyOnGlobalMessage(final String senderName,
			final String date, final String content) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();

				IViewPart globalChatView = page.findViewReference(
						GlobalChatView.ID).getView(false);

				if (globalChatView != null) {
					((GlobalChatView) globalChatView).receiveMessage(
							senderName, content);
				}
			}
		});
	}

	public void doNotifyOnPersonalMessage(final String senderName,
			final String date, final String content) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IEditorPart editor = PrivateChatAction.openEditor(senderName);
				if (editor != null) {
					PrivateChatEditor chatEditor = (PrivateChatEditor) editor;
					chatEditor.receiveMessage(senderName, content);
				}
			}
		});
	}

	public void doNotifyOnAvatarLoginOrLogout(final String avatarName,
			final String whatHappened) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();
				IViewPart roomView = page.findViewReference(RoomView.ID)
						.getView(false);

				if (roomView != null) {
					((RoomView) roomView).getRoomViewer().refresh();
				}

				IViewPart globalChatView = page.findViewReference(
						GlobalChatView.ID).getView(false);

				if (globalChatView != null) {
					((GlobalChatView) globalChatView).receiveSpecialMessage(
							whatHappened, SWT.COLOR_BLUE);
				}
			}
		});
	}

	/******************
	 * Admin callbacks
	 ******************/

	public void doNotifyOnAvatarKick() {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();
				page.closeAllEditors(false);
				page.closePerspective(page.getPerspective(), false, false);
				Client.CALL_BACK_REF.roomName("");

				try {
					PlatformUI.getWorkbench().showPerspective(
							WorldsPerspective.ID,
							PlatformUI.getWorkbench()
									.getActiveWorkbenchWindow());

					MessageDialog
							.openInformation(Display.getCurrent()
									.getActiveShell(), "OWNED",
									"You have been kicked from the room by the administrator");

				} catch (WorkbenchException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void doNotifyOnUserKick() {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();
				page.closeAllEditors(false);
				page.closePerspective(page.getPerspective(), false, false);
				MessageDialog
						.openInformation(Display.getCurrent().getActiveShell(),
								"OWNED",
								"You have been kicked from the application by the administrator");
			}
		});
		PlatformUI.getWorkbench().restart();
	}

	public void doNotifyOnUserConnection() {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();

				IViewPart connectedUserView = page.findViewReference(
						ConnectedUsersView.ID).getView(false);

				if (connectedUserView != null) {
					((ConnectedUsersView) connectedUserView)
							.getConnectedUsersViewer().refresh();
					return;
				}
			}
		});
	}

	public void updateWorldsView(final String roomName) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();

				IViewPart worldsView = page.findViewReference(WorldsView.ID)
						.getView(false);

				if (worldsView != null) {
					Object[] treeElements = ((WorldsView) worldsView)
							.getWorldsViewer().getExpandedElements();
					for (Object o : treeElements) {
						if (o instanceof RoomEntity) {
							RoomEntity r = (RoomEntity) o;
							if (r.getRoom().name.equals(roomName)) {
								((WorldsView) worldsView).getWorldsViewer()
										.refresh(o);
								return;
							}
						}
					}
					((WorldsView) worldsView).getWorldsViewer().refresh();
				}
			}
		});
	}

	public void doNotifyOnGlobalMessageinWatchedRoom(
			final String watchedRoomName, final String senderName,
			final String date, final String content) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();

				RoomGlobalChatEditorInput input = new RoomGlobalChatEditorInput(
						watchedRoomName);
				IEditorPart editor = page.findEditor(input);
				if (editor != null) {
					((RoomGlobalChatEditor) editor).receiveMessage(senderName,
							content);
					return;
				}
				return;
			}
		});
	}

	public void doNotifyAdminOnJoinOrExit(final String leftRoomName,
			final String incomingAvatarName, final String whatHappened) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();

				RoomGlobalChatEditorInput input = new RoomGlobalChatEditorInput(
						leftRoomName);
				IEditorPart editor = page.findEditor(input);
				if (editor != null) {
					((RoomGlobalChatEditor) editor).receiveSpecialMessage(
							whatHappened, SWT.COLOR_GRAY);
					return;
				}
				return;
			}
		});
	}

	public void doNotifyAdminOnAvatarLoginOrLogout(final String roomName,
			final String avatarName, final String whatHappened) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();

				RoomGlobalChatEditorInput input = new RoomGlobalChatEditorInput(
						roomName);
				IEditorPart editor = page.findEditor(input);
				if (editor != null) {
					((RoomGlobalChatEditor) editor).receiveSpecialMessage(
							whatHappened, SWT.COLOR_BLUE);
					return;
				}
				return;
			}
		});
	}
}